const {
  Controller,
} = require('../lib/contorller');
const {
  validateModel,
} = require('../lib/mongo');

module.exports = class ValidateController extends Controller {

  /**
   * 根据_id查询对应的validate
   * 存在则更新
   * 不存在则新增
   * 
   * expression:表达式
   * regexp:正则表达式
   * script:可执行脚本
   */
  async updateKey() {
    const {
      ctx,
    } = this;
    const {
      id,
      expression,
      regexp,
      script,
      json,
      type,
      name,
      describe,
    } = ctx.request.body;
    const value = this._verifyParam({
      type,
      expression,
      regexp,
      script,
      json,
    });
    if (!value) {
      ctx.result.success = false;
      ctx.result.message = '缺少参数';
      return;
    }

    let info = await validateModel.findById(id);
    ctx.result.message = `${!info?'新增':'更新'} validate`;
    if(!info){
      info = await validateModel.create({
        name,
        describe,
      });
    }
    info.type = type;
    info[type] = value;
    const validate = await info.save();
    if (validate.errors) {
      ctx.result.success = false;
      return;
    } 
    ctx.result.success = true;
    const returnObj = {};
    returnObj.id = validate._id;
    returnObj.type = validate.type;
    if (validate.type === 'regexp') {
      returnObj.regexp = validate.regexp.source;
    } else {
      returnObj[validate.type] = validate[validate.type];
    }
    ctx.result.info = returnObj;
  }

  /**
   * 根据传入的_id查询对应的validte
   */
  async getValidateById() {
    const {
      ctx,
    } = this;
    const {
      id,
    } = ctx.request.query;
    if (id) {
      try {
        ctx.result.info = await validateModel.findById(id);
      } catch (err) {
        ctx.result.message = err;
      }
      if (ctx.result.info) {
        ctx.result.success = true;
      } else {
        ctx.result.success = false;
      }
    } else {
      ctx.result.success = false;
      ctx.result.message = 'id必填';
    }
  }

  async getAllValidate() {
    const {
      ctx,
    } = this;
    const info = await validateModel.find({}).lean();
    if (info) {
      ctx.result.success = true;
    }
    ctx.result.info = info.map(item => {
      item.regexp = item.regexp && item.regexp.source;
      item.id = item._id;
      delete item._id;
      return item;
    });
  }

  async deleteValidateById() {
    const {
      ctx,
    } = this;
    const {
      id,
    } = ctx.request.query;
    const info = await validateModel.findByIdAndDelete(id);
    if(!info){
      ctx.result.message = '未找到数据';
      return;
    }
    ctx.result.message = '删除validate';
    ctx.result.success = true;
    ctx.result.info = info;
  }

  _verifyParam({
    type,
    expression,
    regexp,
    script,
    json,
  }) {
    switch (type) {
    case 'expression': {
      if (expression) {
        return expression;
      }
      break;
    }
    case 'regexp': {
      if (regexp) {
        return new RegExp(regexp, 'gm');
      }
      break;
    }
    case 'script': {
      if (script) {
        return script;
      }
      break;
    }
    case 'json': {
      if (json) {
        return json;
      }
      break;
    }
    }
  }
};